﻿using IapService.Entity;
using NHibernate.Criterion;

namespace IapService.Data
{
    public class FavoriteData : Data<Favorite>
    {
        public new Favorite Save(Favorite item)
        {
            var sample = item.Sample;
            var sampleData = new SampleData();
            sampleData.SaveFromFavorite(sample);
            
            using (var session = Session)
            {
                session.SaveOrUpdate(item);
                session.Flush();
            }
            return item;
        }

        public Favorite SaveFromInstanceFavorite(Favorite item)
        {
            using (var session = Session)
            {
                session.SaveOrUpdate(item);
                session.Flush();
            }
            return item;
        }

        public new Favorite Remove(Favorite item)
        {
            InstanceFavorite instanceFavorite;
            using (var session = Session)
            {
                instanceFavorite = session.CreateCriteria<InstanceFavorite>()
                    .Add(Restrictions.Eq("Favorite.Id", item.Id))
                    .UniqueResult<InstanceFavorite>()
                    ;
            }
            var result = new InstanceFavoriteData().Remove(instanceFavorite);
            return result.Favorite;
        }
    }
}